<template>
  <el-dialog
      :title="form.id ? '编辑知识关系' : '新增知识关系'"
      :visible.sync="show"
      width="600px"
      @close="onClose"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="源节点" prop="sourceId">
        <el-select
            v-model="form.sourceId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词搜索"
            :remote-method="remoteSearchSource"
            :loading="sourceLoading"
            style="width: 100%"
            :disabled="!!nodeId"
        >
          <el-option
              v-for="item in sourceOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          >
            <span>{{ item.name }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">
              {{ getNodeTypeName(item.type) }}
            </span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目标节点" prop="targetId">
        <el-select
            v-model="form.targetId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词搜索"
            :remote-method="remoteSearchTarget"
            :loading="targetLoading"
            style="width: 100%"
        >
          <el-option
              v-for="item in targetOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          >
            <span>{{ item.name }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">
              {{ getNodeTypeName(item.type) }}
            </span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关系类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择关系类型" style="width: 100%">
          <el-option label="包含" :value="1"/>
          <el-option label="属于" :value="2"/>
          <el-option label="依赖" :value="3"/>
          <el-option label="相关" :value="4"/>
          <el-option label="引用" :value="5"/>
        </el-select>
      </el-form-item>
      <el-form-item label="关系描述" prop="description">
        <el-input
            type="textarea"
            v-model="form.description"
            :rows="4"
            placeholder="请输入关系描述"
        />
      </el-form-item>
      <el-form-item label="权重" prop="weight">
        <el-input-number
            v-model="form.weight"
            :min="0"
            :max="10"
            :step="0.1"
            :precision="1"
            style="width: 200px"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { http } from "@/api";

export default {
  name: "KnowledgeRelationAdd",
  props: {
    nodeId: {
      type: [String, Number],
      default: null
    }
  },
  data() {
    return {
      show: false,
      sourceLoading: false,
      targetLoading: false,
      sourceOptions: [],
      targetOptions: [],
      form: {
        id: null,
        sourceId: '',
        targetId: '',
        type: '',
        description: '',
        weight: 1
      },
      rules: {
        sourceId: [
          { required: true, message: '请选择源节点', trigger: 'change' }
        ],
        targetId: [
          { required: true, message: '请选择目标节点', trigger: 'change' }
        ],
        type: [
          { required: true, message: '请选择关系类型', trigger: 'change' }
        ],
        description: [
          { required: true, message: '请输入关系描述', trigger: 'blur' }
        ],
        weight: [
          { required: true, message: '请输入权重', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    getNodeTypeName(type) {
      const types = {
        1: '概念',
        2: '定理',
        3: '公式',
        4: '规则',
        5: '设备',
        6: '技术'
      }
      return types[type] || '-'
    },
    remoteSearchSource(query) {
      if (query) {
        this.sourceLoading = true
        http.knowledgeNode.page({ name: query, current: 1, size: 99999 }).then(res => {
          this.sourceOptions = res.data.records
        }).finally(() => {
          this.sourceLoading = false
        })
      } else {
        this.sourceOptions = []
      }
    },
    remoteSearchTarget(query) {
      if (query) {
        this.targetLoading = true
        http.knowledgeNode.page({ name: query, current: 1, size: 99999 }).then(res => {
          this.targetOptions = res.data.records
        }).finally(() => {
          this.targetLoading = false
        })
      } else {
        this.targetOptions = []
      }
    },
    open(row) {
      this.show = true
      if (row) {
        this.form = { ...row }
      } else {
        this.form = {
          id: null,
          sourceId: this.nodeId || '',
          targetId: '',
          type: '',
          description: '',
          weight: 1
        }
      }
    },
    onClose() {
      this.show = false
      this.$refs.form.resetFields()
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const data = { ...this.form }
          if (data.id) {
            http.knowledgeRelation.update(data).then(() => {
              this.$message.success('编辑成功')
              this.onClose()
              this.$emit('get-page')
            })
          } else {
            http.knowledgeRelation.create(data).then(() => {
              this.$message.success('新增成功')
              this.onClose()
              this.$emit('get-page')
            })
          }
        }
      })
    }
  }
}
</script>
